package com.erpoa.controller;

import com.erpoa.annotation.NoRepeatSubmit;
import com.erpoa.entity.ErpAccessoriesData;
import com.erpoa.service.ImaFileService;
import com.erpoa.utils.AjaxResult;
import com.erpoa.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:0xOO
 * @Date: 2018/9/26 0026
 * @Time: 14:42
 */
@Api(value = "ImaFileController", tags = {"erp与oa、科力普的接口"})
@RestController
@RequestMapping("/oaerp/oa")
public class ImaFileController {
    private static final Logger logger = LoggerFactory.getLogger(ImaFileController.class);

    @Autowired
    private ImaFileService imaService;

    @ApiOperation("erp跑批未生成请购单发送邮件提醒")
    @RequestMapping(value = "sendMail",method = RequestMethod.GET,produces = { "application/json;charset=UTF-8" })
    @NoRepeatSubmit(key = "sendMail:arg[0]")
    // @Async(value = "hjSimpleExecutor")
    public String sendMail( @RequestParam(value = "id",required = true)String id){
        return imaService.sendMail(id);
    }

    @ApiOperation("erp邮件发送辅料数据")
    @RequestMapping(value = "sendMailAttachment", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @NoRepeatSubmit(key = "sendMailAttachment:arg[0]")
    @Async(value = "hjSimpleExecutor")
    public String sendMailAttachment(@RequestParam(value = "hrno", required = true) String hrno) {
        return imaService.sendMailAttachment(hrno);
    }


    @ApiOperation("辅料请购获取oa用户id")
    @RequestMapping("getUser")
    public String GetUser( @RequestParam(value = "sqId",required = true)Integer sqId,
                           @RequestParam(value = "ds",required = true)String ds, @RequestParam(value = "workshop",required = true)String workshop,@RequestParam(value = "purchase",required = true)String purchase,@RequestParam(value = "v_gmisc",required = true)String v_gmisc){
        return imaService.count( sqId,  ds,workshop,purchase,v_gmisc).toString();
    }

    @ApiOperation("辅料请购发起流程")
    @RequestMapping(value = "creatFlow",method = RequestMethod.GET,produces = { "application/json;charset=UTF-8" })
    public String CreatFlow(@RequestParam(value = "sqId",required = true)Integer sqId,

                            @RequestParam(value = "ds",required = true)String ds,@RequestParam(value = "workshop",required = true)String workshop,@RequestParam(value = "purchase",required = true)String purchase,@RequestParam(value = "v_gmisc",required = true)String v_gmisc){
        logger.debug("请求发起sqId="+sqId+workshop+purchase+v_gmisc);

        if ("DS2".equals(ds) || "DS1".equals(ds)){
            try {
                return imaService.CreatFlow(sqId,ds,workshop,purchase,v_gmisc);
            } catch (Exception e) {
                e.printStackTrace();
                return "-9";
            }
        }

        return  "-15";
    }


    @ApiOperation("设备厂辅料请购发起流程")
    @RequestMapping(value = "creatFlowEquipment",method = RequestMethod.GET,produces = { "application/json;charset=UTF-8" })
    public String CreatFlowEquipment(@RequestParam(value = "sqId",required = true)Integer sqId,

                            @RequestParam(value = "ds",required = true)String ds,@RequestParam(value = "workshop",required = true)String workshop,@RequestParam(value = "purchase",required = true)String purchase,@RequestParam(value = "v_gmisc",required = true)String v_gmisc){
        logger.debug("请求发起sqId="+sqId+workshop+purchase+v_gmisc);

        if ("DS2".equals(ds) || "DS1".equals(ds)){
            try {
                return imaService.CreatFlowEquipment(sqId,ds,workshop,purchase,v_gmisc);
            } catch (Exception e) {
                e.printStackTrace();
                return "-9";
            }
        }

        return  "-15";
    }



    @ApiOperation("辅料导出接口")
    @RequestMapping(value = "/export", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @NoRepeatSubmit(key = "ImaFileExport:arg[0]")
    public AjaxResult export(@RequestParam(value = "hrno", required = true) Integer hrno) {
        List<ErpAccessoriesData> list = imaService.selectErpAccessoriesData();
        ExcelUtil<ErpAccessoriesData> util = new ExcelUtil<ErpAccessoriesData>(ErpAccessoriesData.class);
        return util.exportExcel(list, "生产辅料");
    }


    @RequestMapping(value = "syncFlow",method = RequestMethod.GET,produces = { "application/json;charset=UTF-8" })
    public String syncFlow(@RequestParam(value = "sqId",required = true)Integer sqId,

                           @RequestParam(value = "ds",required = true)String ds,@RequestParam(value = "workshop",required = true)String workshop,@RequestParam(value = "purchase",required = true)String purchase,@RequestParam(value = "v_gmisc",required = true)String v_gmisc){
      logger.debug("请求发起sqId="+sqId+workshop+purchase+v_gmisc);
        if ("DS2".equals(ds) || "DS1".equals(ds)){

            return imaService.syncFlow(sqId, ds, workshop, purchase, v_gmisc);
        }

        return  "-15";
    }

    @ApiOperation("科力普料号校验接口")
    @RequestMapping(value = "colipuCheck",method = RequestMethod.GET,produces = { "application/json;charset=UTF-8" })
    public String colipuCheck(@RequestParam(value = "sqId",required = true)Integer sqId,@RequestParam(value = "ds",required = true)String ds){
        logger.debug("请求发起sqId="+sqId);
        if ("DS2".equals(ds) || "DS1".equals(ds)){
            return imaService.colipuCheck(sqId,ds);
        }

        return "error";
    }


    @ApiOperation("科力普料号校验接口单查询")
    @RequestMapping(value = "colipuCheckSingle",method = RequestMethod.GET,produces = { "application/json;charset=UTF-8" })
    public String colipuCheckSingle(@RequestParam(value = "itemNo",required = true)String itemNo ){
        logger.debug("请求发起料号="+itemNo);
        try {
            return imaService.colipuCheckSingle(itemNo);
        }catch (Exception e){
            e.printStackTrace();
            return imaService.colipuCheckSingle(itemNo);
          //  return "正在准备数据，请再试一次！如多次发生请联系IT！信息:"+e.getMessage();
        }

    }
}
